package com.java.business.system.database.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.java.business.system.database.service.FixBaseService;
import com.java.core.bean.LowerCaseKeyMap;
import com.java.core.bean.ResultBean;
import com.java.core.utils.ResponseUtils;

@RequestMapping("/system/database/fixbase")
@Controller
public class FixBaseController {

	@Autowired
	private FixBaseService service;

	@RequestMapping("/renderGrid")
	public String renderGrid(HttpServletResponse response, String tablename,
			String keys, ModelMap model) {
		Map<String, Object> cfg = new HashMap<String, Object>();
		List<LowerCaseKeyMap> list = service.getTableColumns(tablename);
		cfg.put("list", list);
		cfg.put("tablename", tablename);
		cfg.put("keys", keys);
		model.put("cfg", JSON.toJSON(cfg));
		return "view/system/database/fixbase/editbase";
	}

	@RequestMapping("/syncBuis")
	public void syncBuis(HttpServletResponse response) {
		ResultBean rb = new ResultBean();
		try {
			int count = service.syncBuis();
			rb.setMsg("操作成功，新增数据" + count + "条！");
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/updateBuis")
	public void updateBuis(HttpServletResponse response,
			HttpServletRequest request) {
		ResultBean rb = new ResultBean();
		try {
			service.updateBuis(request);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/chaifenBuis")
	public void chaifenBuis(HttpServletResponse response,
			HttpServletRequest request) {
		ResultBean rb = new ResultBean();
		try {
			service.chaifenBuis(request);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/deleteBuis")
	public void deleteBuis(HttpServletResponse response,
			HttpServletRequest request) {
		ResultBean rb = new ResultBean();
		try {
			service.deleteBuis(request);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}
}
